<?php
Yii::import('model.UserInfo');
class UserController extends Controller {
		
	public $layout=false;
	
    public function actionLogin()
    {
    	if ($this->isLogin())
    	{
    		header('Location:'.CONF_MAIN_DOMAIN);
    		return;
    	}
    	
    	//登录
    	if (Yii::app()->request->getParam('submit'))
    	{
    		$userName = trim(Yii::app()->request->getParam("user_name"));
    		$password = trim(Yii::app()->request->getParam("password"));
    		$remember = Yii::app()->request->getParam("remember");
    		$verify_code = Yii::app()->request->getParam("verify_code");
    		 
    		if (VERIFY_CODE && strtolower($verify_code) != strtolower($_SESSION['myAdmin_verify_code']))
    		{
    			$this->alert("error",ErrorCode::VERIFY_CODE_WRONG,'/user/login');
    		}
    		 
    		$info = UserInfo::getInstance()->getUserInfoByUserName($userName);
    		 
    		if(!$info)
    		{
    			$this->alert("error",ErrorCode::USER_NOT_EXIST,'/user/login');
    		}
    		
    		if($info['status'] == 0)
    		{
    			$this->alert("error",ErrorCode::BE_PAUSED,'/user/login');
    		}
    		 
    		if(md5($password) !=  $info['password'])
    		{
    			$this->alert("error",ErrorCode::PWD_WRONG,'/user/login');
    		}
    		 
    		$_SESSION['login_status'] = 1;
    		$_SESSION['uid'] = $info['user_id'];
    		$_SESSION['user_group'] = $info['user_group'];
    		$_SESSION['user_role'] = $info['group_role'];
    		$_SESSION['userName'] = $userName;
    		UserInfo::getInstance()->updateUserInfo(array('login_time'=>time()),$info['user_id']);
    		$this->sysLog(array('loginRet'=>'Success','userName'=>$userName,'userId'=>$info['user_id']));
    		header('Location:'.CONF_MAIN_DOMAIN);
    		exit();
    	}
    	$this->render('login');
    }
      
    /**
     * 生成验证码
     */
    public function actionCaptcha()
    {
    	if (! isset ( $_SESSION )) { // 判断session是否开启
    		session_start (); // 开启就session
    	}
    	$width = 70; // 布画宽度
    	$height = 25; // 布画高度
    	$length = 4; // 验证码长度
    	$code = $this->_getcode ( $length ); // 获取随机字符串
    	$_SESSION ['myAdmin_verify_code'] = $code;
    	$img = imagecreate ( $width, $height );
    	$bgcolor = imagecolorallocate ( $img, 240, 240, 240 );
    	$rectangelcolor = imagecolorallocate ( $img, 150, 150, 150 );
    	imagerectangle ( $img, 1, 1, $width - 1, $height - 1, $rectangelcolor ); // 画边框
    	for($i = 0; $i < $length; $i ++) { // 循环写字
    		$codecolor = imagecolorallocate ( $img, mt_rand ( 50, 200 ), mt_rand ( 50, 128 ), mt_rand ( 50, 200 ) );
    		$angle = rand ( - 20, 20 );
    		$charx = $i * 15 + 8;
    		$chary = ($height + 14) / 2 + rand ( - 1, 1 );
    		
    		imagettftext ( $img, 15, $angle, $charx, $chary, $codecolor, WE_ROOT_PATH.'/static/css/font/tahoma.ttf', $code [$i] );
    	}
    	for($i = 0; $i < 20; $i ++) { // 循环画线
    		$linecolor = imagecolorallocate ( $img, mt_rand ( 0, 250 ), mt_rand ( 0, 250 ), mt_rand ( 0, 250 ) );
    		$linex = mt_rand ( 1, $width - 1 );
    		$liney = mt_rand ( 1, $height - 1 );
    		imageline ( $img, $linex, $liney, $linex + mt_rand ( 0, 4 ) - 2, $liney + mt_rand ( 0, 4 ) - 2, $linecolor );
    	}
    	for($i = 0; $i < 100; $i ++) { // 循环画点
    		$pointcolor = imagecolorallocate ( $img, mt_rand ( 0, 250 ), mt_rand ( 0, 250 ), mt_rand ( 0, 250 ) );
    		imagesetpixel ( $img, mt_rand ( 1, $width - 1 ), mt_rand ( 1, $height - 1 ), $pointcolor );
    	}
    	ob_clean ();
    	header ( 'Content-type:image/png' );
    	imagepng ( $img );
    }   
    
    public function actionLogout()
    {
    	unset($_SESSION['login_status']);
    	unset($_SESSION['uid']);
    	unset($_SESSION['userName']); 
    	//$this->render('logout');
    	$this->exitWithSuccess("退出登录成功！", "/");
    }
    
  	private function _getcode($length) { // 生成php随机数
    	$pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '; // 字符池
    	for($i = 0; $i < $length; $i ++) {
    		$key .= $pattern {mt_rand ( 0, 35 )};
    	}
    	return $key;
    }
}
?>